package cn.sciento.dd.service.impl;

import javax.annotation.Resource;
import cn.sciento.dd.dto.AddFileDTO;
import cn.sciento.dd.dto.DefaultResultDTO;
import cn.sciento.dd.dto.GetEnterprisesSpaceDTO;
import cn.sciento.dd.dto.GetEnterprisesSpaceInfoResultDTO;
import cn.sciento.dd.dto.UploadFileDTO;
import cn.sciento.dd.dto.UploadFileTransactionDTO;
import cn.sciento.dd.dto.UploadMediaDTO;
import cn.sciento.dd.service.DingFileStorageService;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

@Service
public class DingFileStorageServiceImp implements DingFileStorageService {
  @Resource(name = "wdRestTemplate")
  protected RestTemplate restTemplate;
  
  public UploadMediaDTO uploadMedia(String accessToken, String type, String media) {
    return (UploadMediaDTO)this.restTemplate.postForObject("https://oapi.dingtalk.com/media/upload?access_token=" + accessToken + "&type=" + type, new HttpEntity(media, (MultiValueMap)buildHttpHeaders()), UploadMediaDTO.class);
  }
  
  public DefaultResultDTO sendFileToUser(String accessToken, String agent_id, String userid, String media_id, String file_name) {
    return (DefaultResultDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/cspace/add_to_single_chat?access_token=" + accessToken + "&agent_id=" + agent_id + "userid&=" + userid + "&media_id=" + media_id + "file_name=" + file_name, DefaultResultDTO.class);
  }
  
  public AddFileDTO addFile(String accessToken, String agent_id, String code, String media_id, String space_id, String folder_id, String name, Boolean overwrite) {
    return (AddFileDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/cspace/add?access_token=" + accessToken + "&agent_id=" + agent_id + "code&=" + code + "&media_id=" + media_id + "&space_id=" + space_id + "&folder_id" + folder_id + "&name" + name + "&overwrite" + overwrite, AddFileDTO.class);
  }
  
  public GetEnterprisesSpaceDTO getEnterprisesSpace(String accessToken, String domain, String agent_id) {
    return (GetEnterprisesSpaceDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/cspace/get_custom_space?access_token=" + accessToken + "&agent_id=" + agent_id + "&domain=" + domain, GetEnterprisesSpaceDTO.class);
  }
  
  public GetEnterprisesSpaceInfoResultDTO getEnterprisesSpaceInfo(String accessToken, String domain, String agent_id) {
    return (GetEnterprisesSpaceInfoResultDTO)this.restTemplate.postForObject("https://oapi.dingtalk.com/cspace/uesd_info?access_token=" + accessToken + "&domain=" + domain + "&agent_id" + agent_id, new HttpEntity(null, (MultiValueMap)buildHttpHeaders()), GetEnterprisesSpaceInfoResultDTO.class);
  }
  
  public DefaultResultDTO getAuthorizedUserSpace(String accessToken, String agent_id, String domain, String type, String userid, String path, String fileids, Integer duration) {
    return (DefaultResultDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/cspace/grant_custom_space?access_token=" + accessToken + "&agent_id=" + agent_id + "&domain=" + domain + "&type=" + type + "&userid=" + userid + "&path=" + path + "&fileids=" + fileids + "&duration=" + duration, GetEnterprisesSpaceDTO.class);
  }
  
  public UploadFileDTO singleUploadFile(String accessToken, String agent_id, Integer file_size) {
    return (UploadFileDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/file/upload/single?access_token=" + accessToken + "&agent_id=" + agent_id + "&file_size=" + file_size, UploadFileDTO.class);
  }
  
  public UploadFileTransactionDTO openUploadFileTransaction(String accessToken, String agent_id, Integer file_size, Integer chunk_numbers) {
    return (UploadFileTransactionDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/file/upload/transaction?access_token=" + accessToken + "&agent_id=" + agent_id + "&file_size=" + file_size + "&chunk_numbers=" + chunk_numbers, UploadFileTransactionDTO.class);
  }
  
  public DefaultResultDTO chunkUploadFile(String accessToken, String agent_id, String upload_id, Integer chunk_sequence) {
    return (DefaultResultDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/file/upload/chunk?access_token=" + accessToken + "&agent_id=" + agent_id + "&upload_id=" + upload_id + "&chunk_sequence=" + chunk_sequence, DefaultResultDTO.class);
  }
  
  public UploadFileDTO uploadFileTransaction(String accessToken, String agent_id, Integer file_size, Integer chunk_numbers, String upload_id) {
    return (UploadFileDTO)this.restTemplate.getForObject("https://oapi.dingtalk.com/file/upload/transaction?access_token=" + accessToken + "&agent_id=" + agent_id + "&file_size=" + file_size + "&chunk_numbers=" + chunk_numbers + "&upload_id=" + upload_id, UploadFileDTO.class);
  }
  
  protected HttpHeaders buildHttpHeaders() {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.set("Content-Type", "application/json");
    return httpHeaders;
  }
}
